public class Test {
    /*
    #### 多源 BFS

1. [542. 01 矩阵](https://leetcode.cn/problems/01-matrix/)
class Solution {
    int[] dx = { 0, 0, 1, -1 };
    int[] dy = { 1, -1, 0, 0 };

    public int[][] updateMatrix(int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        int[][] dist = new int[m][n];

        Queue<int[]> q = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (mat[i][j] == 0) {
                    q.add(new int[] { i, j });
                } else {
                    dist[i][j] = -1;//标记未搜索的地方 并且自增代替step
                }
            }
        }
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0], b = t[1];
            for (int k = 0; k < 4; k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && dist[x][y] == -1 && mat[x][y] ==1 ) {
                    dist[x][y] = dist[a][b]+1;
                    q.add(new int[] { x, y });
                }
            }
        }

        return dist;
    }
}
2. [1020. 飞地的数量](https://leetcode.cn/problems/number-of-enclaves/)
class Solution {
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    public int numEnclaves(int[][] grid) {
        int m = grid.length , n  = grid[0].length;
        int count = 0;

        boolean[][] vis = new boolean[m][n];
        Queue<int[]> q = new LinkedList<>();

        for(int i = 0 ; i < m ; i++) {
            for(int j = 0; j < n ;j++) {
                if(i == 0 || i == m-1 || j == 0 || j == n-1) {
                    if(grid[i][j] == 1) {
                        q.add(new int[]{i,j});
                        vis[i][j] = true;
                        grid[i][j] = 0;
                    }
                }
            }
        }
        while(!q.isEmpty()) {
                int[] t = q.poll();
                int a = t[0] , b = t[1];
                for(int k = 0; k < 4 ;k++) {
                    int x = a + dx[k];
                    int y = b + dy[k];
                    if(x >= 0 && x < m && y >=0 && y < n && !vis[x][y] && grid[x][y] == 1 ) {
                        grid[x][y] = 0;
                        q.add(new int[]{x,y});
                        vis[x][y] = true;
                    }
                }
        }
        for(int i = 0 ; i< m;i++) {
            for(int j = 0;j<n;j++) {
                if(grid[i][j] == 1) {
                    count++;
                }
            }
        }
        return count;
    }
}
3. [1765. 地图中的最高点](https://leetcode.cn/problems/map-of-highest-peak/)
class Solution {
    int[] dx = { 0, 0, 1, -1 };
    int[] dy = { 1, -1, 0, 0 };
    public int[][] highestPeak(int[][] isWater) {
        int m = isWater.length, n = isWater[0].length;
        int[][] dist = new int[m][n];// 标记是否出现过 并且初始化为-1 自增
        Queue<int[]> q = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (isWater[i][j] == 1) {
                    q.add(new int[] { i, j });
                    dist[i][j] = 0;
                } else {
                    dist[i][j] = -1;
                }
            }
        }
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0], b = t[1];
            for (int k = 0; k < 4; k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >=0 && y >=0 && x <m && y < n && dist[x][y] == -1 ) {
                    dist[x][y] = dist[a][b]+1;
                    q.add(new int[]{x,y});
                }
            }
        }

        return dist;
    }
}
4. [1162. 地图分析](https://leetcode.cn/problems/as-far-from-land-as-possible/)
class Solution {
    int[] dx = {0,0,1,-1};
    int[] dy = {1,-1,0,0};
    public int maxDistance(int[][] grid) {
        int m = grid.length ,n = grid[0].length;
        boolean[][] vis = new boolean[m][n];
        Queue<int[]> q = new LinkedList<>();
        for(int i = 0; i < m ;i++) {
            for(int j = 0; j<n;j++) {
                if(grid[i][j] == 1) {
                    q.add(new int[]{i,j});
                    grid[i][j] = 0;
                    vis[i][j] = true;
                }
            }
        }
        while(!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0] , b = t[1];
            for(int k = 0; k <4 ;k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if( x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 0) {
                    grid[x][y] = grid[a][b] + 1;
                    vis[x][y] = true;
                    q.add(new int[]{x,y});
                }
            }
        }
        int max = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n ;j++) {
               max = Math.max(max,grid[i][j]);
            }
        }
        return max == 0 ? -1 :max;
    }
}
     */
}
